View Javadoc

1   package org.apache.maven.surefire.testng.conf;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.reflect.Method;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.maven.surefire.booter.ProviderParameterNames;
30  import org.apache.maven.surefire.testset.TestSetFailedException;
31  import org.testng.TestNG;
32  import org.testng.xml.XmlSuite;
33  
34  public abstract class AbstractDirectConfigurator
35      implements Configurator
36  {
37      final Map setters;
38  
39      AbstractDirectConfigurator()
40      {
41          Map options = new HashMap();
42          // options.put( ProviderParameterNames.TESTNG_GROUPS_PROP, new Setter( "setGroups", String.class ) );
43          // options.put( ProviderParameterNames.TESTNG_EXCLUDEDGROUPS_PROP, new Setter( "setExcludedGroups", String.class
44          // ) );
45          options.put( "junit", new Setter( "setJUnit", Boolean.class ) );
46          options.put( ProviderParameterNames.THREADCOUNT_PROP, new Setter( "setThreadCount", int.class ) );
47          options.put( "usedefaultlisteners", new Setter( "setUseDefaultListeners", boolean.class ) );
48          this.setters = options;
49      }
50  
51      public void configure( TestNG testng, Map options )
52          throws TestSetFailedException
53      {
54          System.out.println( "\n\n\n\nCONFIGURING TESTNG\n\n\n\n" );
55          // kind of ugly, but listeners are configured differently
56          final String listeners = (String) options.remove( "listener" );
57          // DGF In 4.7, default listeners dump XML files in the surefire-reports directory,
58          // confusing the report plugin.  This was fixed in later versions.
59          testng.setUseDefaultListeners( false );
60          configureInstance( testng, options );
61          // TODO: we should have the Profile so that we can decide if this is needed or not
62          testng.setListenerClasses( loadListenerClasses( listeners ) );
63      }
64  
65      public void configure( XmlSuite suite, Map options )
66          throws TestSetFailedException
67      {
68          Map filtered = filterForSuite( options );
69          configureInstance( suite, filtered );
70      }
71  
72  
73      protected Map filterForSuite( Map options )
74      {
75          Map result = new HashMap();
76          addPropIfNotNull( options, result, ProviderParameterNames.PARALLEL_PROP );
77          addPropIfNotNull( options, result, ProviderParameterNames.THREADCOUNT_PROP );
78          return result;
79      }
80  
81      private void addPropIfNotNull( Map options, Map result, String prop )
82      {
83          if ( options.containsKey( prop ) )
84          {
85              result.put( prop, options.get( prop ) );
86          }
87      }
88  
89      private void configureInstance( Object testngInstance, Map options )
90      {
91          for ( Iterator it = options.entrySet().iterator(); it.hasNext(); )
92          {
93              Map.Entry entry = (Map.Entry) it.next();
94              String key = (String) entry.getKey();
95              Object val = entry.getValue();
96  
97              Setter setter = (Setter) setters.get( key );
98              if ( setter != null )
99              {
100                 try
101                 {
102                     setter.invoke( testngInstance, val );
103                 }
104                 catch ( Exception ex )
105                 {
106                     throw new RuntimeException( "Cannot set option " + key + " with value " + val, ex );
107                 }
108 
109             }
110         }
111     }
112 
113     public static List loadListenerClasses( String listenerClasses )
114         throws TestSetFailedException
115     {
116         if ( listenerClasses == null || "".equals( listenerClasses.trim() ) )
117         {
118             return new ArrayList();
119         }
120 
121         List classes = new ArrayList();
122         String[] classNames = listenerClasses.split( "\\s*,\\s*(\\r?\\n)?\\s*" );
123         for ( int i = 0; i < classNames.length; i++ )
124         {
125             String className = classNames[i];
126             Class clazz = loadClass( className );
127             classes.add( clazz );
128         }
129 
130         return classes;
131     }
132 
133     public static Class loadClass( String className )
134         throws TestSetFailedException
135     {
136         try
137         {
138             return Class.forName( className );
139         }
140         catch ( Exception ex )
141         {
142             throw new TestSetFailedException( "Cannot find listener class " + className, ex );
143         }
144     }
145 
146     public static final class Setter
147     {
148         private final String setterName;
149 
150         private final Class paramClass;
151 
152         public Setter( String name, Class clazz )
153         {
154             this.setterName = name;
155             this.paramClass = clazz;
156         }
157 
158         public void invoke( Object target, Object value )
159             throws Exception
160         {
161             Method setter = target.getClass().getMethod( this.setterName, new Class[]{ this.paramClass } );
162             if ( setter != null )
163             {
164                 setter.invoke( target, new Object[]{ convertValue( value ) } );
165             }
166         }
167 
168         Object convertValue( Object value )
169         {
170             if ( value == null )
171             {
172                 return value;
173             }
174             if ( this.paramClass.isAssignableFrom( value.getClass() ) )
175             {
176                 return value;
177             }
178 
179             if ( Boolean.class.equals( this.paramClass ) || boolean.class.equals( this.paramClass ) )
180             {
181                 return Boolean.valueOf( value.toString() );
182             }
183             if ( Integer.class.equals( this.paramClass ) || int.class.equals( this.paramClass ) )
184             {
185                 return new Integer( value.toString() );
186             }
187 
188             return value;
189         }
190     }
191 }